package com.base.game;

import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import com.base.engine.error.EMADErrorCode;
import com.base.engine.error.MADErrorHandler;
import com.base.engine.system.EDirection;
import com.base.game.Game;

public final class InputHandler {

	public static final float MOUSE_SENSITIVITY = 0.05f ;
	
	public static void getKeyboardInput(Game g){
		if(!(g == null) && g.isRunning()){
			while(Keyboard.next()){
				if(Keyboard.getEventKeyState()){
					switch(Keyboard.getEventKey()){
					}
				}
			}
			
			if(Keyboard.isKeyDown(Keyboard.KEY_W))
				g.move(EDirection.NORTH); 
			if(Keyboard.isKeyDown(Keyboard.KEY_A))
				g.move(EDirection.WEST);  
			if(Keyboard.isKeyDown(Keyboard.KEY_S))
				g.move(EDirection.SOUTH); 
			if(Keyboard.isKeyDown(Keyboard.KEY_D))
				g.move(EDirection.EAST); 
			if(Keyboard.isKeyDown(Keyboard.KEY_SPACE))
				g.move(EDirection.UP); 
			if(Keyboard.isKeyDown(Keyboard.KEY_LSHIFT))
				g.move(EDirection.DOWN); 
			
			if(Keyboard.isKeyDown(Keyboard.KEY_T))
				g.moveTerrainLimit(EDirection.NORTH); 
			if(Keyboard.isKeyDown(Keyboard.KEY_G))
				g.moveTerrainLimit(EDirection.SOUTH); 
			if(Keyboard.isKeyDown(Keyboard.KEY_F))
				g.moveTerrainLimit(EDirection.WEST); 
			if(Keyboard.isKeyDown(Keyboard.KEY_H))
				g.moveTerrainLimit(EDirection.EAST); 
			
			
		}else
			MADErrorHandler.throwException(EMADErrorCode.GAME_REF, new NullPointerException("Invalid game - Game reference is NULL!")); 
	}
	
	public static void getMouseInput(Game g){
		
		if(!(g == null) && g.isRunning()){
			while(Mouse.next()){
				float dx = Mouse.getEventDX(); 
				float dy = Mouse.getEventDY();
				
				g.yaw(dx * MOUSE_SENSITIVITY); 
				g.pitch(-dy * MOUSE_SENSITIVITY); 
				if(Mouse.getEventButtonState()){
					switch(Mouse.getEventButton()){
					case 0: if(!Mouse.isGrabbed()) Mouse.setGrabbed(true); 						
//								g.getTerrain().getSkybox().getWeatherSystem().changeWeather(EWeatherCondition.values()[new Random().nextInt(3)]); 		

//								float mx = Mouse.getEventX(); 
//								float my = Mouse.getEventY();
//								glRenderMode(GL_SELECT);
//								glMatrixMode(GL_PROJECTION); 
//								glLoadIdentity(); 
//								gluPickMatrix(mx,my,5,5,MADEngine.viewport); 
//								glFrustum(-MADEngine.WIDTH/2,MADEngine.WIDTH/2,-MADEngine.HEIGHT/2,MADEngine.HEIGHT/2,MADBasicGL.NEAR_CLIPPING,MADBasicGL.FAR_CLIPPING); 
//								glInitNames(); 
//								glMatrixMode(GL_MODELVIEW);
							break ; 
					}
				}
				Mouse.setGrabbed(true);
			}
		}else
			MADErrorHandler.throwException(EMADErrorCode.GAME_REF, new NullPointerException("Invalid game - Game reference is NULL!")); 
	}
	
}
